<!DOCTYPE html>
<meta charset=utf-8>
<title>Cross-realm IDBIndex methods from detached iframe work as expected</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=resources/support.js></script>

<body>
<script>
"use strict";
const INDEX_LOWER = 1000;
const INDEX_UPPER = 1001;
const INDEX_RANGE = IDBKeyRange.bound(INDEX_LOWER, INDEX_UPPER);

const testCases = [
    {
        methodName: "get",
        arguments: [INDEX_LOWER],
        validateResult: e => {
            assert_equals(e.target.result.indexedKey, INDEX_LOWER);
        },
    },
    {
        methodName: "getKey",
        arguments: [INDEX_UPPER],
        validateResult: e => {
            assert_equals(e.target.result, INDEX_UPPER);
        },
    },
    {
        methodName: "getAll",
        arguments: [INDEX_RANGE],
        validateResult: e => {
            assert_array_equals(e.target.result.map(v => v.indexedKey), [INDEX_LOWER, INDEX_UPPER]);
        },
    },
    {
        methodName: "getAllKeys",
        arguments: [INDEX_RANGE],
        validateResult: e => {
            assert_array_equals(e.target.result, [INDEX_LOWER, INDEX_UPPER]);
        },
    },
    {
        methodName: "count",
        arguments: [INDEX_RANGE],
        validateResult: e => {
            assert_equals(e.target.result, 2);
        },
    },
    {
        methodName: "openCursor",
        arguments: [],
        validateResult: e => {
            const cursor = e.target.result;
            assert_true(cursor instanceof IDBCursor);
            assert_equals(cursor.value.indexedKey, INDEX_LOWER);
        },
    },
    {
        methodName: "openKeyCursor",
        arguments: [],
        validateResult: e => {
            const cursor = e.target.result;
            assert_true(cursor instanceof IDBCursor);
            assert_equals(cursor.key, INDEX_LOWER);
        },
    },
];

for (const testCase of testCases) {
    async_test(t => {
        const iframe = document.createElement("iframe");
        iframe.onload = t.step_func(() => {
            const method = iframe.contentWindow.IDBIndex.prototype[testCase.methodName];
            iframe.remove();

            let db;
            const open_rq = createdb(t);
            open_rq.onupgradeneeded = t.step_func(e => {
                db = e.target.result;
                const objectStore = db.createObjectStore("store");
                objectStore.createIndex("index", "indexedKey");
                objectStore.add({ indexedKey: INDEX_LOWER }, INDEX_LOWER);
                objectStore.add({ indexedKey: INDEX_UPPER }, INDEX_UPPER);
            });

            open_rq.onsuccess = t.step_func(() => {
                const index = db.transaction("store", "readonly", {durability: 'relaxed'}).objectStore("store").index("index");
                const rq = method.call(index, ...testCase.arguments);
                rq.onsuccess = t.step_func_done(e => {
                    testCase.validateResult(e);
                });
            });
        });
        document.body.append(iframe);
    }, `Cross-realm IDBIndex::${testCase.methodName}() method from detached <iframe> works as expected`);
}
</script>
